import java.util.List;

import ar.com.coonocer.joy.mdg.xmi.core.Association;

import ar.com.coonocer.joy.mdg.templatehelpers.AssociationUtil;
import ar.com.coonocer.joy.mdg.templatehelpers.AttributeUtil;
import ar.com.coonocer.joy.mdg.templatehelpers.NamingUtil;
import ar.com.coonocer.joy.mdg.xmi.core.Attribute;
import ar.com.coonocer.joy.mdg.xmi.core.Class
import ar.com.coonocer.joy.mdg.xmi.model.Package

public class XMIClassHelper{

	public static String getXMI(Class clazz){
		StringBuilder sb = new StringBuilder();
		//
		sb.append("				<UML:Class name=\"${NamingUtil.toUpperCamelCase(clazz?.name)}\" xmi.id=\"${getXmiIdForClass(clazz)}\" \r\n");
		sb.append("					visibility=\"public\" isSpecification=\"false\" isRoot=\"false\" \r\n");
		sb.append("					isLeaf=\"false\" isAbstract=\"false\" isActive=\"false\">\r\n");
		//
		sb.append("					<UML:ModelElement.stereotype>\r\n");
		sb.append("						<UML:Stereotype xmi.idref=\""+XMIGenerator.util.getStereotypeRefId("Class", "JoyEntity")+"\"/>\r\n");
		sb.append("					</UML:ModelElement.stereotype>\r\n");
		//
		sb.append("							<UML:ModelElement.taggedValue>\r\n");
		sb.append(getXMIForTaggedValues(clazz));
		sb.append("							</UML:ModelElement.taggedValue>\r\n");
		//
		sb.append("					<UML:Classifier.feature>\r\n");
		//
		// Genera atributos que no participan en asociaciones.
		clazz.feature?.each{att->
			// Por cada atributo
			if (att instanceof Attribute){
				sb.append(XMIAttributeHelper.getXMI(clazz, att));
			}
		}
		//
		// Genera atributos en base a las asociaciones.
		sb.append(XMIAttributeHelper.getXMIForAttributesFromAssociations(clazz));
		//
		sb.append("					</UML:Classifier.feature>\r\n");
		//
		sb.append("				</UML:Class>\r\n");
		//
		return sb.toString();
	}


	public static String getXmiIdForClass(Class clazz){
		return "Class_"+NamingUtil.toUpperCamelCase(clazz?.name);
	}

	private static String getXMIForTaggedValues(Class clazz){
		StringBuilder sb = new StringBuilder();
		//
		sb.append(getXMIForTaggedValue("entity.table",clazz.name));
		sb.append(getXMIForTaggedValue("entity.schema","*"));
		sb.append(getXMIForTaggedValue("entity.catalog","*"));
		sb.append(getXMIForTaggedValue("entity.persistenceUnit.catalog","*"));
		sb.append(getXMIForTaggedValue("entity.roles.create","*"));
		sb.append(getXMIForTaggedValue("entity.roles.read","*"));
		sb.append(getXMIForTaggedValue("entity.roles.edit","*"));
		sb.append(getXMIForTaggedValue("entity.roles.delete","*"));
		//
		return sb.toString();
	}


	public static String getXMIForTaggedValue(String taggedValueName, Object taggedValueValue){
		return XMIGenerator.getXMIForTaggedValue("Class", taggedValueName, taggedValueValue);
	}
}